package com.lagou.controller;

import com.lagou.domain.Menu;
import com.lagou.domain.ResponseResult;
import com.lagou.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    // 查询所有菜单信息
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {

        List<Menu> list = menuService.findAllMenu();
        return new ResponseResult(true, 200, "查询所有菜单信息成功", list);

    }

    // 回显菜单信息
    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(Integer id) {

        Map<String, Object> map = new HashMap<>();
        List<Menu> list = menuService.findSubMenuListByPid(-1);
        ResponseResult responseResult;
        // 根据Id的值判断当前是更新还是添加操作
        if (id == -1) {
            // 添加操作，回显信息中不需要查询menu信息

            // 封装map
            map.put("menuInfo", null);
            map.put("parentMenuList", list);
            responseResult = new ResponseResult(true, 200, "添加回显成功", map);
        } else {

            // 修改操作 回显所有menu信息
            Menu menu = menuService.findMenuById(id);
            map.put("menuInfo", menu);
            map.put("parentMenuList", list);
            responseResult=  new ResponseResult(true, 200, "修改回显成功", map);
        }
        return responseResult;
    }
}
